﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.Data;
using HZ.BidSystem.Model.View;

public partial class Company_GoodsBargain : System.Web.UI.Page, IAccess
{
    string action = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        action = Request.QueryString["action"] == null ? "bargain" : Request.QueryString["action"].ToLower();

        int b = BLL_Factory.IBLL_Common.GetGoodsBargain(int.Parse(CurrentAccount.Account.AccountId.ToString()));
         //HZ.Web.WebUtil.Alert(b.ToString(), this);
        if (b == 1)
        {
            // 验证报价密码
            //if (!CurrentAccount.IsCheckBidPwd)
            //{
            //    Response.Redirect(string.Format("CheckBidPwd.aspx?action={0}", action));
            //}

            if (!CurrentProject.IsBargainSwitch)
            {
                HZ.Web.MessageBox.Show("议价开关未开启，暂时无法报价。");
            }

            if (CurrentProject.InBargainTime < 0)
            {
                HZ.Web.MessageBox.Show("议价时间未到，请稍后！");
            }

            if (CurrentProject.InBargainTime > 0)
            {
                HZ.Web.MessageBox.Show("议价时间已到！");
            }
        }
        else
        {
            HZ.Web.MessageBox.Show("本次招标，您无议价品种！");
        }
        if (!IsPostBack)
        {
            bidinfo.Text = BLL_Factory.IBLL_Manage.GetConfigurationByKey("BargainDes").Value;
            BindData();
        }
       
    }

    /// <summary>
    /// 确认
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButtonGet_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        if (btn.CommandName == "Yes")
        { 
            string company_goods = btn.CommandArgument.Split(',')[0];  
            string CompanyID_TB = btn.CommandArgument.Split(',')[1];
            View_BargainPrice2 goods = new View_BargainPrice2(); //记录日志用
            goods.Company_Goods = company_goods;
            goods.CompanyId_Tb =Convert.ToInt32(CompanyID_TB);
            goods.CheckStatus =1; 
            int CheckSecces = BLL_Factory.IBLL_Company.UpdateGoodsBarin(goods);
            if (CheckSecces == 1)
            {
                HZ.Common.log4netManager.LogInfo("确定要进行议价工作", string.Format("确定要进行议价工作：企业编号：{0}，报价IP：{1}，报价产品：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, company_goods), null);
                HZ.Web.WebUtil.Alert("操作成功！", this); 
            }
            else
            {
                HZ.Web.WebUtil.Alert("操作失败，请检查数据！", this);
            }
            BindData();
        }
    }

    /// <summary>
    /// 拒绝
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void LinkButtonUNGet_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        if (btn.CommandName == "No")
        { 
            string company_goods = btn.CommandArgument.ToString();
            View_BargainPrice2 goods = new View_BargainPrice2(); //记录日志用
            goods.Company_Goods = company_goods;
            goods.CheckStatus = 2; 
            int CheckSecces = BLL_Factory.IBLL_Company.UpdateGoodsBarin(goods);
            if (CheckSecces == 1)
            {
                HZ.Common.log4netManager.LogInfo("确定要执行直接进行挂网操作", string.Format("确定要执行直接进行挂网操作：企业编号：{0}，报价IP：{1}，报价产品：{2}", CurrentAccount.Account.AccountName, CurrentAccount.ClientIP, company_goods), null);
                HZ.Web.WebUtil.Alert("操作成功！", this); 
            }
            else
            {
                HZ.Web.WebUtil.Alert("操作失败，请检查数据！", this);
            }
            BindData();
        }
    }



    protected void btnSearch_Click(object sender, EventArgs e)
    {
        pager1.CurrentPage = 1;
        BindData();
    }
    protected void BindData()
    {
        int totalrecord;
        string append = "1=1";
        if (ddlBidStatus.SelectedValue != "全部")
        {
            append = string.Format(@" CheckStatus={0}", ddlBidStatus.SelectedValue);
        }
        DataTable dt = BLL_Factory.IBLL_Company.GetGoodsBarin(new HZ.BidSystem.Model.QBidPrice() { CompanyId_Tb = CurrentAccount.Account.AccountId, SortFields = "ProductName", ProductName = txtProdcutName.Text.Trim(), Model = txtModel.Text.Trim(), Outlookc = txtOutlookc.Text.Trim(), Append = append }, pager1.CurrentPage, pager1.PageSize, out totalrecord, CurrentAccount.BidPwd, CurrentAccount.RandPwd);
        pager1.RecordCount = totalrecord;
        gvbidprice.DataSource = dt;
        gvbidprice.DataBind(); 
    }

    protected void pager1_PageChanging(object sender, HZ.Web.PageChaningEventArgs e)
    {
        BindData();
    }
    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Company);
            return list;
        }

    }

    #endregion
    protected void gvbidprice_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //   // CheckBox chkbox = e.Row.FindControl("chkConfirm") as CheckBox;
        //    RadioButtonList rablt = e.Row.FindControl("rblisok") as RadioButtonList;
        //    var datasource = e.Row.DataItem as View_BargainPrice;

        //    if (datasource.BargainEncryptPrice != null && datasource.ExpertPrice != null && !datasource.BargainEncryptPrice.ToString().Equals("") && datasource.ExpertPrice.Value>0)
        //    { 
        //        if (datasource.BargainDecPrice.Value > 0) rablt.Items[0].Selected = true;
        //        if (datasource.BargainDecPrice.Value < 1) rablt.Items[1].Selected = true;
        //    }
        //}
    }

    /// <summary>
    /// 获取状态
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public string GetBarState(object o)
    {
        string id = o.ToString(), value = "";

        switch (id)
        {
            case null:
                value = "未处理";
                break;
            case "":
                value = "未处理";
                break;
            case "0":
                value = "未处理";
                break;
            case "1":
                value = "议价";
                break;
            case "2":
                value = "不议价";
                break;
        }
        return value;
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        BindData();
    }
}
